package com.bookrecommendation.client.facebook;

import java.util.HashMap;

import com.bookrecommendation.client.GWTEntryPoint;
import com.bookrecommendation.client.facebook.sdk.FBCore;
import com.bookrecommendation.client.facebook.sdk.JSOModel;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class FacebookFriends {

	public static HashMap<String, String> facebookFriendsID = new HashMap<String, String>();

	public FacebookFriends(final FBCore fbCore) {

		fbCore.api("/me/friends", new AsyncCallback<JavaScriptObject>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(JavaScriptObject result) {

				JSOModel jso = result.cast();

				JsArray array = jso.getArray("data");

				GWTEntryPoint.friendsAmount = array.length();

				for (int i = 0; i < array.length(); i++) {

					JSOModel j = array.get(i).cast();

					String id = j.get("id");

					String name = j.get("name");

					new FacebookFriendBooks(fbCore, id, name, false);
				}
			}
		});

	}
}
